package cn.tedu.epidemic.webapi.service.impl;

import cn.tedu.community.commons.excepion.ServiceException;
import cn.tedu.community.commons.pojo.health.dto.HealthDTO;
import cn.tedu.community.commons.pojo.health.dto.UpdateResponseDTO;
import cn.tedu.community.commons.pojo.health.entity.Health;
import cn.tedu.community.commons.pojo.health.vo.HealthVO;
import cn.tedu.community.commons.restful.JsonPage;
import cn.tedu.community.commons.restful.ServiceCode;
import cn.tedu.epidemic.service.HealthService;
import cn.tedu.epidemic.service.PhotoUploadService;
import cn.tedu.epidemic.webapi.mapper.HealthMapper;
import cn.tedu.epidemic.webapi.security.CurrentUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper mapper;
    @Autowired
    private PhotoUploadService uploadService;

    @Override
    public void insert(HealthDTO healthDTO) {
        Long userId = CurrentUser.getUserId();

        int latest = mapper.countUserIdLatest(userId);
        if (latest > 0) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "您今日已完成健康报备，请勿重复报备！");
        }

        Health health = new Health();
        BeanUtils.copyProperties(healthDTO, health);
        health.setUserId(userId);
        int insert = mapper.insert(health);
        if (insert != 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "服务器繁忙，请稍后重试！");
        }
    }

    @Override
    public void checkReportDay() {
        int latest = mapper.countUserIdLatest(CurrentUser.getUserId());
        if (latest < 1) {
            throw new ServiceException(ServiceCode.HEALTH_NEED, "请完成每日健康报备！");
        }
    }

    @Override
    public void delete(Long id) {
        HealthVO healthVO = healthStandard(id, "删除失败，该报备已不存在！");
        int delete = mapper.deleteById(id);
        if (delete != 1) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "服务器繁忙，请稍后重试！");
        }
        uploadService.deletePhoto(healthVO.getHealthQrcode());
    }

    @Override
    public void updateById(Long id, HealthDTO healthDTO) {
        HealthVO healthVO = healthStandard(id, "修改失败，该报备已不存在！");
        Health health = new Health();
        BeanUtils.copyProperties(healthDTO, health);
        health.setId(id);
        health.setReviewResponse("0");
        int update = mapper.updateById(health);
        if (update != 1) {
            throw new ServiceException(ServiceCode.ERR_UPDATE, "服务器繁忙，请稍后重试！");
        }

        if (!healthVO.getHealthQrcode().equals(healthDTO.getHealthQrcode())) {
            uploadService.deletePhoto(healthVO.getHealthQrcode());
        }
    }

    @Override
    public HealthVO healthStandard(Long id) {
        return healthStandard(id,"查询健康报备详情失败，该报备已不存在！");
    }

    @Override
    public JsonPage<HealthVO> healthListByUserId(Integer page, Integer pageSize) {
        Long userId = CurrentUser.getUserId();
        PageHelper.startPage(page,pageSize);
        List<HealthVO> healthVOS = mapper.healthListByUserId(userId);
        if (healthVOS.isEmpty()) {
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,"您暂未报备每日健康信息！");
        }
        return JsonPage.restPage(new PageInfo<>(healthVOS));
    }

    @Override
    public void updateResponse(Long id, UpdateResponseDTO responseDTO) {
        healthStandard(id,"提交审核失败，该报备已不存在！");
        if (responseDTO.getResponse().equals("0")){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"不允许只输入数字0，请重新输入！");
        }
        Health health = new Health();
        health.setId(id);
        health.setReviewResponse(responseDTO.getResponse());
        int update = mapper.updateById(health);
        if (update!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"服务器繁忙，请稍后重试！");
        }
    }

    @Override
    public JsonPage<HealthVO> ListAuditedOrNot(Integer AuditedOrNot, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<HealthVO> healthVOS = null;
        if (AuditedOrNot==0){
            healthVOS =  mapper.ListUnaudited();
        }else {
            healthVOS = mapper.ListAudited();
        }
        if (healthVOS.isEmpty()){
            String[] emptyMessage = {"暂无未审核健康报备！","暂无已审核健康报备！"};
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,emptyMessage[AuditedOrNot]+"请稍后重试！");
        }
        return JsonPage.restPage(new PageInfo<>(healthVOS));
    }

    private HealthVO healthStandard(Long id, String notFoundMessage) {
        HealthVO healthVO = mapper.healthStandard(id);
        if (healthVO == null) {
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, notFoundMessage);
        }
        return healthVO;
    }
}
